package trading.algo.APIConnectivityTask;

import java.util.Timer;
import java.util.TimerTask;

import trading.algo.TimerKillerTask;
import trading.bo.IBAccessorBO;

public class APIConnectMasterTask extends TimerTask {

    private IBAccessorBO ibAccessorBO;
    
    public APIConnectMasterTask(IBAccessorBO ibAccessorBO) {
        this.ibAccessorBO = ibAccessorBO;
    }
    
    @Override
    public void run() {
        Timer timer = new Timer();
        int taskID = 0;
        
        IBConnectionTask ibConnectionTask = new IBConnectionTask(ibAccessorBO);
        timer.schedule(ibConnectionTask, taskID * 1000);
        taskID += IBConnectionTask.secondsNeeded();
        
        MarketDataFarmConnectionTask marketDataFarmConnectionTask = new MarketDataFarmConnectionTask(this.ibAccessorBO);
        timer.schedule(marketDataFarmConnectionTask, taskID * 1000);
        taskID += MarketDataFarmConnectionTask.secondsNeeded();
        
        HistoricalDataFarmConnectionTask historicalDataFarmConnectionTask = new HistoricalDataFarmConnectionTask(ibAccessorBO);
        timer.schedule(historicalDataFarmConnectionTask, taskID * 1000);
        taskID += HistoricalDataFarmConnectionTask.secondsNeeded();
        
        TimerKillerTask timerKillerTask = new TimerKillerTask(timer);
        timer.schedule(timerKillerTask, taskID * 1000);
    }
    
    public static int secondsNeeded() {
        return IBConnectionTask.secondsNeeded() + MarketDataFarmConnectionTask.secondsNeeded() + HistoricalDataFarmConnectionTask.secondsNeeded();
    }
}
